package com.easy.common.util;
import cn.hutool.jwt.JWT;
import cn.hutool.jwt.JWTValidator;
import cn.hutool.jwt.signers.JWTSignerUtil;
import io.jsonwebtoken.*;
import io.jsonwebtoken.security.Keys;

import javax.crypto.SecretKey;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class JWTUtil {

	public static final String  SECRET_KEY ="Easy";
	private static final long EXPIRATION = 86400000; // 24小时

	public static String generateToken(Map map) {
		return cn.hutool.jwt.JWTUtil.createToken(map,SECRET_KEY.getBytes());


//		System.out.println(SECRET_KEY);
//		return Jwts.builder()
//				.setSubject(username)
//				.claim("roles", roles)
//				.setIssuedAt(new Date())
//				.setExpiration(new Date(System.currentTimeMillis() + EXPIRATION))
//				.signWith(SignatureAlgorithm.HS256, SECRET_KEY)
//				.compact();
	}

	public static Object parseToken(String token) {
		JWT jwt=cn.hutool.jwt.JWTUtil.parseToken(token);
		System.out.println(jwt);
		return jwt;
	}
	public static boolean verifToken(String token){
		if (!cn.hutool.jwt.JWTUtil.verify(token,SECRET_KEY.getBytes())){
			throw new RuntimeException("签名异常");
		}
		try {
				JWTValidator.of(token).validateDate();
		}catch (Exception e){
			throw  new RuntimeException("签名过期");
		}
		return true;
	}



//	public static void main(String[] args) {
//		Map map=new HashMap();
//		map.put("name","zhangsan");
////		String token=generateToken(map);
//		String token="eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJuYW1lIjoiemhhbmdzYW4ifQ.8SFUSgzP9cCbCrD8HpwEioFUL1ndsCU8M9iJr4DO3c0";
//		System.out.println(token);
//		System.out.println(parseToken(token));
//		System.out.println(verifToken(token));
}